package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.MemberIndexModule;
import cn.com.connext.scrm.service.MemberIndexModuleService;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.web.rest.vm.MemberIndexModulePageVM;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 第三方调用接口
 */
@RestController
@RequestMapping("/api/memberindexmodule/")
public class MemberIndexModuleResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(MemberIndexModuleResource.class);
    @Autowired
    private MemberIndexModuleService memberIndexModuleService;

    public MemberIndexModuleResource() {
        super("MemberIndexModule");
    }

    /**
     * 添加或修改首页
     */
    @ApiOperation(value = "添加或修改会员节首页", notes = "添加或修改会员节首页")
    @PostMapping(value = "/saveModule")
    public JsonResult addProduct(@Valid @RequestBody MemberIndexModule entity){
        JsonResult jsonResult =new JsonResult();
            //新增
        try{
             jsonResult = memberIndexModuleService.save(entity);
        }catch (Exception e){
             jsonResult.setMessage("系统异常");
             jsonResult.setState(-1);
             log.info("异常信息------------"+e.getMessage());
        }
        return  jsonResult;
    }

    /**
     * 删除会员节模块
     */
    @ApiOperation(value = "删除会员节模块", notes = "删除会员节模块")
    @PostMapping(value = "/deleteModule")
    public JsonResult deleteModule(@RequestBody MemberIndexModule entity){
        JsonResult jsonResult =new JsonResult();
        try{
            jsonResult = memberIndexModuleService.deleteModule(entity);
        }catch (Exception e){
            jsonResult.setMessage("系统异常");
            jsonResult.setState(-1);
            log.info("异常信息------------"+e.getMessage());
        }
        return  jsonResult;
    }

    /**
     * 删除会员节模块
     */
    @ApiOperation(value = "分页查询会员节模块列表", notes = "分页查询会员节模块列表")
    @PostMapping(value = "/findByPage")
    public JsonResult findByPage(@Valid @RequestBody MemberIndexModulePageVM vm){
        JsonResult result =new JsonResult();
        try{
            if(vm ==null || vm.getPage() ==null || vm.getPageSize() ==null){
                result.setState(-1);
                result.setMessage("参数无能为空");
                return  result;
            }
            Page<MemberIndexModule> page = memberIndexModuleService.findByPage(vm);
            result.setData(page);
            result.setState(0);
        }catch (Exception e){
            result.setMessage("系统异常");
            result.setState(-1);
            log.info("异常信息------------"+e.getMessage());
        }
        return  result;
    }


    /**
     * 根据id查询模块具体信息
     */
    @ApiOperation(value = "根据id查询模块具体信息", notes = "根据id查询模块具体信息")
    @PostMapping(value = "/findById")
    public JsonResult findById(@RequestBody MemberIndexModule  entity){
        JsonResult result =new JsonResult();
        try{
            result = memberIndexModuleService.findById(entity.getId());
        }catch (Exception e){
            result.setMessage("系统异常");
            result.setState(-1);
            log.info("异常信息------------"+e.getMessage());
        }
        return  result;
    }

    /**
     * 根据id查询模块具体信息
     */
    @ApiOperation(value = "启用停用", notes = "启用停用")
    @PostMapping(value = "/updateIsEnable")
    public JsonResult updateIsEnable(@RequestBody MemberIndexModule  entity){
        JsonResult result =new JsonResult();
        try{
            result = memberIndexModuleService.updateIsEnable(entity.getId(),entity.getIsEnable());
        }catch (Exception e){
            result.setMessage("系统异常");
            result.setState(-1);
            log.info("异常信息------------"+e.getMessage());
        }
        return  result;
    }

}

